package com.open.basic;

import com.open.basic.constant.BizConstant;

import java.io.Serial;
import java.io.Serializable;


/**
 * @author: system
 * @Date: 2024/4/11 23:38
 * @Version: V1.0
 * @description: desc:基础响应对象
 */
public class BaseResponse<T> implements Serializable {


    @Serial
    private static final long serialVersionUID = 5173191173471454835L;
    /**
     * 是否业务成功
     */
    private boolean success;

    /**
     * 是否成功代码："bizSuccess":成功，其他失败
     * @see BizConstant
     */
    private String code;

    /**
     * 是否成功代码对应的信息
     */
    private String message;

    /**
     * 用于异常时，存放堆栈信息
     */
    private String causedBy = "";

    /**
     * 业务数据
     */
    private T data = null;


    /**
     * 默认构造函数
     */
    public BaseResponse() {
        super();
    }

    public BaseResponse(BizConstant bizConstant) {
        this.code = bizConstant.getCode();
        this.message = bizConstant.getMessage();
        this.success = bizConstant.compareTo(BizConstant.BIZ_SUCCESS) == 0;
    }

    /**
     * 构造函数
     *
     * @param success 是否成功
     * @param bizConstant    是否成功代码
     * @param message 是否成功信息
     */
    public BaseResponse(boolean success, BizConstant bizConstant, String message) {
        super();
        this.success = success;
        this.code = bizConstant.getCode();
        this.message = message;
    }

    /**
     * 构造函数
     *
     * @param success  是否成功
     * @param bizConstant  是否成功代码
     * @param message  是否成功信息
     * @param causedBy 扩展信息 ，异常时存异常储堆栈信息
     * @see BizConstant
     */
    public BaseResponse(boolean success, BizConstant bizConstant, String message, String causedBy) {
        super();
        this.success = success;
        this.code = bizConstant.getCode();
        this.message = message;
        this.causedBy = causedBy;
    }

    /**
     * 构造函数
     *
     * @param success  是否成功
     * @param bizConstant     是否成功代码
     * @param message  是否成功信息
     * @param causedBy 扩展信息 ，异常时存异常储堆栈信息
     * @param data     业务执行结果
     * @see BizConstant
     */
    public BaseResponse(boolean success, BizConstant bizConstant, String message, String causedBy, T data) {
        super();
        this.success = success;
        this.code = bizConstant.getCode();
        this.message = message;
        this.causedBy = causedBy;
        this.data = data;
    }

    public <T> BaseResponse<T> successful() {
        return success();
    }

    public <T> BaseResponse<T> successful(T result) {
        return success(result);
    }

    public static <T> BaseResponse<T> success() {
        return new BaseResponse<>(BizConstant.BIZ_SUCCESS);
    }

    /**
     * 构造函数
     * result 业务执行结果
     */
    public static <T> BaseResponse<T> success(T result) {
        BaseResponse<T> response = new BaseResponse<>(BizConstant.BIZ_SUCCESS);
        response.setData(result);
        return response;
    }

    /**
     * 构造函数
     *
     * @param failCode    失败代码
     * @param failMessage 失败信息
     */
    public static <T> BaseResponse<T> fail(String failCode, String failMessage) {
        return new BaseResponse<T>(false, BizConstant.getByCode(failCode), failMessage);
    }

    public BaseResponse<T> failed(String failMessage) {
        return fail(failMessage);
    }

    public BaseResponse<T> failed(String failCode, String failMessage) {
        return fail(failCode, failMessage);
    }

    /**
     * 构造函数
     *
     * @param failMessage 失败信息
     */
    public static <T> BaseResponse<T> fail(String failMessage) {
        return new BaseResponse<T>(false, BizConstant.BIZ_EXCEPTION, failMessage);
    }

    /**
     * 构造函数
     *
     * @param message 校验时数据不存在信息
     */
    public static <T> BaseResponse<T> invalidate(String message) {
        return new BaseResponse<>(false, BizConstant.BIZ_FAILURE, message);
    }

    public BaseResponse<T> invalidation(String message) {
        return invalidate(message);
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCausedBy() {
        return causedBy;
    }

    public void setCausedBy(String causedBy) {
        this.causedBy = causedBy;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}

